// pages/home/device/fc01.js
var app = getApp();

var page_did = "";
var page_index = 0;
var page_size = 20;
var page_refresh = false;
var page_timer = null;
var page_total = 20;
var query_size = 20;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    device: null,
    device_state: 0,
    device_mode: 0,
    device_rate:0,
    ishide:"",
    isrun: false,
    alarm: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    page_timer = null;
    //console.log(options);
    page_did = options.did;
    //console.log("page_did:" + page_did)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var token = app.globalData.Token;//app.globalData.Token;
    if (token == null || token == "") {
      wx.navigateTo({
        url: '/pages/user/login'
      })
      return;
    }
    page_timer = null;
    page_refresh = true;
    this.getdevice();
    this.ontimer();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
  },
  //获取设备数据
  getdevice: function (e) {
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    //console.log("getdevice");
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;

    wx.request({
      url: app.globalData.Url + "Api/Device/GetDevice", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        that.setData({ device: res.data.data });
        var snap = res.data.data.Snap;
        for (var i in snap) {
          var v = snap[i].Value;
          if (snap[i].V == "V1") {
            that.setData({ device_mode: v });
            that.setData({ ishide: v == 0||v==2 ? "hide" : "" });
            continue;
          }
          if (snap[i].V == "V2") {
            //console.log(" 状态:" + snap[i].Value && 1);
            that.setData({ device_state: v });
            that.setData({ isrun: v & 1});
            continue;
          }
          if (snap[i].V == "V3") {
            that.setData({ device_rate: snap[i].Value });
            continue;
          }
        }
      }
    })
  },
  //定时方法
  ontimer: function () {
    var that = this;
    page_timer = setTimeout(function () {
      //console.log("----ontimer 定时器...----");
      if (page_refresh) {
        that.getdevice();
        that.ontimer();
      }
    }, 10000);
  },
  //设置
  onsetting: function (e) {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
    //console.log(this.data.device.Name);
    //console.log("did:" + e.currentTarget.dataset.did + " page_did:" + page_did);
    wx.navigateTo({
      url: '/pages/home/device/setting?did=' + page_did + '&name=' + this.data.device.Info.Name
    })
  },
  //解绑设备
  unbind: function () {
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/UnBind", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        wx.reLaunch({
          url: '/pages/home/index'
        })
      }
    })
  },
  //解绑设备事件
  onunbind: function (e) {
    var that = this;
    wx.showModal({
      title: '解绑',
      content: '确定解绑该设备?',
      success: function (res) {
        if (res.confirm) {
          that.unbind();
        }
      }
    })
  },
  //qrcode
  onqrcode: function (e) {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
    wx.navigateTo({
      url: '/pages/home/qr?did=' + page_did
    })
  },
  /**
       * 页面上拉触底事件的处理函数
       */
  onReachBottom: function () {
    if (page_total >= query_size) {
      query_size = query_size + page_size;
    }
    //console.log("page_total:" + page_total + " page_size:" + page_size);
  },
  //onchart
  onchart: function (e) {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
    wx.navigateTo({
      url: '/pages/home/device/chart?did=' + page_did + '&name=' + this.data.device.Info.Name + "&cmd=0"
    })
  },
  //控制指令
  fccmd: function (e) {
    var value = e.detail.value;
    var that = this;

    var token = app.globalData.Token;
    if (token == null || token == "") return;

    var c = {};
    var d = {};
    d.State = 1;
    d.Rate = value;

    c.action = "SetOperate";
    c.args = d;
    var cmd = JSON.stringify(c);
    //console.log("value:" + value);
    wx.request({
      url: app.globalData.Url + "Api/Device/DeviceCommand", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
        cmd: cmd,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        var msg = "";
        if (res.data.result) {
          //setTimeout(that.getdevice, 15000);
        }
        else {
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },
  //停止控制指令
  fccmd_stop: function (e) {
    var value = e.detail.value;
    var that = this;

    var token = app.globalData.Token;
    if (token == null || token == "") return;

    var c = {};
    var d = {};
    d.Cmd = 6;
    d.Address = 8192;
    d.Data = 4;

    c.action = "SetCommand";
    c.args = d;
    var cmd = JSON.stringify(c);
    //console.log("cmd:" + cmd);
    wx.request({
      url: app.globalData.Url + "Api/Device/DeviceCommand", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
        cmd: cmd,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        var msg = "";
        if (res.data.result) {
          setTimeout(that.getdevice, 10000);
        }
        else {
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },
  //运行控制指令
  fccmd_start: function (e) {
    var value = 3000;
    var that = this;

    var token = app.globalData.Token;
    if (token == null || token == "") return;

    var c = {};
    var d = {};
    d.State = 1;
    d.Rate = value;

    c.action = "SetOperate";
    c.args = d;
    var cmd = JSON.stringify(c);
    //console.log("value:" + value);
    wx.request({
      url: app.globalData.Url + "Api/Device/DeviceCommand", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
        cmd: cmd,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        var msg = "";
        if (res.data.result) {
          //setTimeout(that.getdevice, 15000);
        }
        else {
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },
})